package com.wei.tang.widget.dialog;

import com.wei.tang.R;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class DialogSmallCancel extends Dialog {

	public DialogSmallCancel(Context context) {
		super(context);
	}

	public DialogSmallCancel(Context context, int theme) {
		super(context, theme);
	}

	public static class Builder {
		private int dialogLayout;
		private Context context;
		private String title;
		private boolean isShield = false;
		private boolean isHidenNgative = false;
		private String message;
		private String positiveButtonText;
		private String negativeButtonText;
		private View contentView;
		private DialogInterface.OnClickListener positiveButtonClickListener;
		private DialogInterface.OnClickListener negativeButtonClickListener;

		public Builder(Context context, int dialogLayout) {
			this.context = context;
			this.dialogLayout = dialogLayout;
		}

		public Builder setMessage(String message) {
			this.message = message;
			return this;
		}

		public Builder setMessage(int message) {
			this.message = (String) context.getText(message);
			return this;
		}

		public Builder setTitle(int title) {
			this.title = (String) context.getText(title);
			return this;
		}

		public Builder setShield(boolean _shield) {
			this.isShield = _shield;
			return this;
		}

		public Builder setHidenNgative(boolean _hidenNgative) {
			this.isHidenNgative = _hidenNgative;
			return this;
		}

		public Builder setTitle(String title) {
			this.title = title;
			return this;
		}

		public Builder setContentView(View v) {
			this.contentView = v;
			return this;
		}

		public Builder setPositiveButton(int positiveButtonText, DialogInterface.OnClickListener listener) {
			this.positiveButtonText = (String) context.getText(positiveButtonText);
			this.positiveButtonClickListener = listener;
			return this;
		}

		public Builder setPositiveButton(String positiveButtonText, DialogInterface.OnClickListener listener) {
			this.positiveButtonText = positiveButtonText;
			this.positiveButtonClickListener = listener;
			return this;
		}

		public Builder setNegativeButton(int negativeButtonText, DialogInterface.OnClickListener listener) {
			this.negativeButtonText = (String) context.getText(negativeButtonText);
			this.negativeButtonClickListener = listener;
			return this;
		}

		public Builder setNegativeButton(String negativeButtonText, DialogInterface.OnClickListener listener) {
			this.negativeButtonText = negativeButtonText;
			this.negativeButtonClickListener = listener;
			return this;
		}

		public DialogSmallCancel create() {
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			final DialogSmallCancel dialog = new DialogSmallCancel(context, R.style.Dialog);
			View layout = inflater.inflate(dialogLayout, null);
			dialog.addContentView(layout, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
			((TextView) layout.findViewById(R.id.small_confirm_dialog_title)).setText(title);
			if (positiveButtonText != null) {
				((Button) layout.findViewById(R.id.small_confirm_dialog_positiveButton)).setText(positiveButtonText);
				if (positiveButtonClickListener != null) {
					((Button) layout.findViewById(R.id.small_confirm_dialog_positiveButton))
							.setOnClickListener(new View.OnClickListener() {
								public void onClick(View v) {
									positiveButtonClickListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
								}
							});
				}
			} else {
				layout.findViewById(R.id.small_confirm_dialog_positiveButton).setVisibility(View.GONE);
			}
			if (isHidenNgative) {
				((Button) layout.findViewById(R.id.small_confirm_dialog_negativeButton)).setVisibility(View.GONE);
			} else if (negativeButtonText != null && !isHidenNgative) {
				((Button) layout.findViewById(R.id.small_confirm_dialog_negativeButton)).setText(negativeButtonText);
				if (negativeButtonClickListener != null) {
					((Button) layout.findViewById(R.id.small_confirm_dialog_negativeButton))
							.setOnClickListener(new View.OnClickListener() {
								public void onClick(View v) {
									negativeButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
								}
							});
				}
			} else {
				layout.findViewById(R.id.small_confirm_dialog_negativeButton).setVisibility(View.GONE);
			}
			if (message != null) {
				((TextView) layout.findViewById(R.id.small_confirm_dialog_message)).setText(message);
			}
			if (isShield) {
				dialog.setCancelable(false);
				dialog.setOnKeyListener(new OnKeyListener() {
					@Override
					public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
						if (keyCode == KeyEvent.KEYCODE_BACK) {
							return true;
						} else {
							return false;
						}
					}
				});
			}
			dialog.setContentView(layout);
			return dialog;
		}
	}
}
